package lia.meetlucene;

/**
 * Copyright Manning Publications Co.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific lan
 */

import static lia.util.Constants.*;
import static lia.util.Utilities.buildIndexDir;

import org.apache.log4j.Logger;
import org.apache.lucene.document.Document;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.Directory;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.analysis.standard.StandardAnalyzer;

import java.io.File;
import java.io.IOException;

// From chapter 1

/**
 * This code was originally written for Erik's Lucene intro java.net article
 * 
 * @author Wenlong Meng(wenlong.meng@gmail.com)
 * @version 1.0 at 2011/08/01 for lucene 3.3.0
 */
public class Searcher {

	// local variables
	/**
	 * log
	 */
	private static final Logger logger = Logger.getLogger("commonLogger");

	/**
	 * test entry
	 * 
	 * @param args
	 * @throws IllegalArgumentException
	 * @throws IOException
	 * @throws ParseException
	 */
	public static void main(String[] args) throws IllegalArgumentException,
			IOException, ParseException {
		logger.debug("Begin: test main Searcher...");
		/*
		 * if (args.length != 2) { throw new
		 * IllegalArgumentException("Usage: java " + Searcher.class.getName() +
		 * " <index dir> <query>"); }
		 * 
		 * String indexDir = args[0]; //1 String q = args[1];
		 */// 2
		String q = "FreeBSD";
		search(buildIndexDir("1"), q);
		logger.debug("End: test main Searcher!");
	}

	public static void search(String indexDir, String q) throws IOException,
			ParseException {
		logger.debug("the index dir: " + indexDir);
		Directory dir = FSDirectory.open(new File(indexDir)); // 3
		IndexSearcher is = new IndexSearcher(dir); // 3

		QueryParser parser = new QueryParser(LUCENE_VERSION, // 4
				"contents", // 4
				new StandardAnalyzer( // 4
						LUCENE_VERSION)); // 4
		Query query = parser.parse(q); // 4
		logger.debug(query);
		long start = System.currentTimeMillis();
		TopDocs hits = is.search(query, 10); // 5
		long end = System.currentTimeMillis();

		logger.debug("Found " + hits.totalHits + // 6
				" document(s) (in " + (end - start) + // 6
				" milliseconds) that matched query '" + // 6
				q + "':"); // 6

		for (ScoreDoc scoreDoc : hits.scoreDocs) {
			Document doc = is.doc(scoreDoc.doc); // 7
			logger.debug(doc.get("fullpath")); // 8
		}
		logger.debug("close index search...");
		is.close(); // 9
	}
}

/*
 * #1 Parse provided index directory #2 Parse provided query string #3 Open
 * index #4 Parse query #5 Search index #6 Write search stats #7 Retrieve
 * matching document #8 Display filename #9 Close IndexSearcher
 */